package com.xiaoyang.forum.controller;

import cn.hutool.core.util.StrUtil;
import com.xiaoyang.forum.common.code.BaseController;
import com.xiaoyang.forum.common.code.Result;
import com.xiaoyang.forum.common.code.ResultCode;
import com.xiaoyang.forum.common.util.JWTUtil;
import com.xiaoyang.forum.domain.vo.UserFollowVO;
import com.xiaoyang.forum.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoYang
 * @since 2025-09-17
 */
@RestController
@RequestMapping("/user-follow")
public class UserFollowController extends BaseController {

    @Autowired
    private UserFollowService userFollowService;

    /**
     * 关注和取消关注
     * @param token  关注者(登录用户)
     * @param followedUserId 被关注者
     * @return
     */
    @PostMapping("/follow")
    public Result<Void> follow(@RequestHeader(required = false) String token,
                               Long followedUserId) {
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN.getMessage());
        }

        if (followedUserId == null) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }
        long followUserId = (long) JWTUtil.getUserIdFromToken(token);
        return toR(userFollowService.follow(followUserId, followedUserId));
    }

    /**
     * 获取关注列表
     * @return
     */
    @GetMapping("/followed-user-list")
    public Result<List<UserFollowVO>> getFollowedUserList(@RequestHeader(required = false) String token) {
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN.getMessage());
        }
        long followUserId = (long) JWTUtil.getUserIdFromToken(token);
        return Result.success(userFollowService.getFollowedUserList(followUserId));
    }

    /**
     * 获取粉丝列表
     * @return
     */
    @GetMapping("/follow-user-list")
    public Result<List<UserFollowVO>> getFollowUserList(@RequestHeader(required = false) String token) {
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN.getMessage());
        }
        long followedUserId = (long) JWTUtil.getUserIdFromToken(token);
        return Result.success(userFollowService.getFollowUserList(followedUserId));
    }
}
